#include "Common.h"
#include "Posix/AppLibPosix_Msg.hpp"

void Flush(void)
{
    char c;

    while((c = getchar()) != '\n' && c != EOF);
}

SINT32 main(SINT32 argc, CHAR **args)
{
    SINT32 i = 0;
    SINT32 iRet = 0;
    CHAR szBuff[1024] = {0}; 
    ULONG ulRet = STAT_SUCCESS;
    APP_LIB_POSIX_MQ_s stPosixMq = {0};
	APP_LIB_POSIX_MQ_MSG_s *pstMsg = (APP_LIB_POSIX_MQ_MSG_s *)szBuff;

	if (argc != 2)
	{
		LOG_ERROR("Usage: ./PosixMq_Cli <Path>");
		return -1;
	}

    APP_LIB_POSIX_MQ_INIT(stPosixMq, args[1], 512, 5);

    ulRet = AppLibPosix_MqOpen(&stPosixMq);
    if (ulRet != STAT_SUCCESS)
    {
        return ulRet;
    }

	while (1)
	{
        LOG_INFO("To exit, enter \"quit\"");
		memset(szBuff, 0, sizeof(szBuff));
		
		iRet = scanf("%s", (CHAR *)pstMsg->data);
        UNUSED(iRet);
		Flush();

		pstMsg->prio = i++;
		pstMsg->len = strlen((CHAR *)pstMsg->data) + 1;
		ulRet = AppLibPosix_MqSend(&stPosixMq, pstMsg, NULL);
		if (ulRet != STAT_SUCCESS)
		{
			continue;
		}

		LOG_INFO("Send <%s> Successfully\n", (char *)pstMsg->data);
		if (strcmp((char *)pstMsg->data, "quit") == 0)
		{
			break;
		}
	}

    return 0;
}
